package com.ch.web.mq;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 监听回调队列的事件
 */
@Component
public class MqListern implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        doCallInfo();
        System.out.println("rabbitCustom自制的监听器启动完毕");
    }

    private void doCallInfo() {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    ResultMessage take = RabbitMqCustomTemplate.getCallQueue().take();
                    if (take.getRegister().getStatus()) {
                        System.out.println(take.getRegister().getId() + "编号消息成功");
                    } else {
                        //失败的信息  放到数据库里面去 日志查看 为设么
                        //将这个信息可以重试一次
                        System.out.println(take.getRegister().getId() + "编号消息失败，原因是:" + take.getMsg());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }
}
